#!/bin/bash
########################################################################
# script name:  list_hmc.sh
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2012/05/30    0.1     MDUMONT         initial
########################################################################
usage()
{
cat << STOP

usage: $0 [options]

This script is used for ....

OPTIONS:
   -q               Query: columns values to query
   -c               Filter columns to display
   -u               Update table with fresh values
   -s               Script output (without separators)
   -c               Show console command
   -A               Show activate command
   -D               Show de-activate command

Available columns:
${COLUMNS[@]}

Ex.:
$0 -q ${COLUMNS[0]}=saprmt02
$0 -q ${COLUMNS[2]}=MPU-9117-MMB-SN10378CP
$0 -q ${COLUMNS[3]}=active,${COLUMNS[2]}=!MPU-9117-MMB-SN10378CP
$0 -q ${COLUMNS[2]}=MPU-9117-MMB-SN10378CP -c ${COLUMNS[0]}
STOP
}
#######################################
# Get parameters
#######################################
command -v /usr/bin/mysql/mysql > /dev/null 2>&1 && MYSQL=/usr/bin/mysql/mysql
command -v /opt/freeware/bin/mysql/mysql > /dev/null 2>&1 && MYSQL=/opt/freeware/bin/mysql/mysql
command -v /usr/bin/mysql > /dev/null 2>&1 && MYSQL=/usr/bin/mysql
if [ -z $MYSQL ]; then echo "Need mysql client installed"; exit 1; fi
if [ -z $QOPTION ]; then QOPTION="-e"; else QOPTION="-Bse"; fi
MYSQL_COMMAND="$MYSQL -hsaplxs01 -uguest -pguest opmgt"
COLUMNS=( $($MYSQL_COMMAND -Bse "SHOW COLUMNS FROM hmc" | awk '{print $1}') )
#######################################
while getopts q:c:usCAD OPTION
do
	case $OPTION in
		q)
			WHERE=( $(echo $OPTARG | tr '[A-Z]' '[a-z]' | sed 's/,/ /g') )
		;;
		c)
			SELECT=$(echo $OPTARG | tr '[A-Z]' '[a-z]')
		;;
		u)
			UPDATE=yes
		;;
		s)
			QOPTION="-Bse"
		;;
		C)
			CONSOLE=yes
		;;
		A)
			ACTIVATE=yes
		;;
		D)
			DEACTIVATE=yes
		;;
		\?)
			usage
			exit 1
		;;
		*)
			usage
			exit 1
		;;
esac
done
#######################################
# Variables and environnement
#######################################
if [ $OPTIND = 1 ]; then usage; exit 1; fi
HMC=mhae770a
#######################################
# MAIN
#######################################
#######################################
# FUNCTIONS
#######################################
function build_query {
		firsttime=y
		for element in ${WHERE[@]}
		do
			e_COLUMN=$(echo $element | cut -d= -f1)
			e_VALUE=$(echo $element | cut -d= -f2)
			for i in ${COLUMNS[@]}
			do
				if [ "$e_COLUMN" = "$i" ]
				then
					if [ "${e_VALUE:0:1}" = "!" ]; then NOT=NOT; e_VALUE=$(echo $e_VALUE | cut -c2-); else NOT=""; fi
					if [ "$e_VALUE" = "all" ] && [ "$i" = "${COLUMNS[0]}" ]; then e_VALUE="%"; fi
					if [ "$firsttime" = "y" ]
					then
						QUERY="WHERE $e_COLUMN $NOT LIKE '%$e_VALUE%' "
						firsttime=n
					else
						QUERY="$QUERY AND $e_COLUMN $NOT LIKE '%$e_VALUE%' "
					fi
				fi
			done
		done
		#if [ -z "$SELECT" ]; then SELECT=${COLUMNS[0]}; fi
		#if [ "$SELECT" = "all" ]; then SELECT=\*; fi
		if [ "$SELECT" = "all" ] || [ -z "$SELECT" ]; then SELECT=\*; fi
}
#######################################
if [ ! -z $UPDATE ]
then
	if [ `whoami` != root ]; then echo Please run this script as root or using sudo; exit; fi
	SYSTEMS=$(sudo ssh -x -2 -q sap_viewer@$HMC "lssyscfg -r sys -F name")
	sudo -u saunix $MYSQL -v -e "TRUNCATE opmgt.hmc;"
	firsttime=y
	for SYSTEM in $(echo $SYSTEMS | tr ' ' '\n')
	do
		while IFS=, read hmc system server state os_version logical_serial_num default_profile rmc_state rmc_ipaddr
		do
			if [ "$firsttime" = "y" ]
			then
				QUERY="INSERT INTO opmgt.hmc (server,hmc,system,state,os_version,logical_serial_num,default_profile,rmc_state,rmc_ipaddr) VALUES "
				firsttime=n
			fi
			QUERY="$QUERY('$server','$hmc','$system','$state','$os_version','$logical_serial_num','$default_profile','$rmc_state','$rmc_ipaddr'),"
		done < <(sudo ssh -x -2 -q sap_viewer@$HMC "lssyscfg -r lpar -m $SYSTEM -F name,state,os_version,logical_serial_num,default_profile,rmc_state,rmc_ipaddr" | sed "s/^/$HMC,$SYSTEM,/g")
	done
	QUERY=$(echo $QUERY | sed 's/,$//')
	sudo -u saunix $MYSQL -v -e "$QUERY"
else
	build_query
	if [ ! -z $CONSOLE ] || [ ! -z $ACTIVATE ] || [ ! -z $DEACTIVATE ]
	then 
			while IFS=, read hmc system name default_profile
			do
				if [ ! -z $CONSOLE ]; then /bin/echo sudo ssh -x -t -e none hscroot@$hmc mkvterm -m $system -p $name; fi
				if [ ! -z $ACTIVATE ]; then /bin/echo sudo ssh -x -t -e none hscroot@$hmc chsysstate -m $system -r lpar -n $name -f \'$default_profile\' -o on; fi
				if [ ! -z $DEACTIVATE ]; then /bin/echo sudo ssh -x -t -e none hscroot@$hmc chsysstate -m $system -r lpar -n $name -o shutdown --immed; fi
			done < <($MYSQL_COMMAND -Bse "SELECT SQL_CACHE DISTINCT hmc,system,server,default_profile FROM hmc $QUERY" | awk '{print $1","$2","$3","$4}')
	else
		$MYSQL_COMMAND $QOPTION "SELECT SQL_CACHE DISTINCT $SELECT FROM hmc $QUERY ORDER BY ${COLUMNS[0]} ASC "
	fi
fi
#######################################
exit 0
